import Vue from 'vue'
import Router from 'vue-router'
// 登录页
import Login from '@/view/Login'
// 管理后台首页
import Home from '@/view/Home'
// 欢迎页
import Welcome from '@/view/Welcome'
// 用户中心
import Users from '@/view/user'
// 用户中心 - 修改资料
import UsersUpdete from '@/view/user/user'
// 商品管理
import Goods from '@/view/goods'
import Cate from '@/view/goods/cate'


Vue.use(Router)

const router =  new Router({
  routes: [
    {path: '/',
    component: Home , 
    redirect: '/welcome',
      children: [{
        path: '/welcome', 
        component: Welcome
      },
      { path:'/users', component: Users },
      { path:'/users/updete', component: UsersUpdete },
      { path:'/categories', component: Cate },
      { path:'/goods', component: Goods },

    ]
    },
    {path: '/login',component: Login}
    
  ]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 表示通过
  if (to.path === '/login') return next()

  const tokenStr = window.sessionStorage.getItem('token')

  if (!tokenStr) return next('/login')
  next()
})

export default router